#pragma once


#include"RequestObj.h"
#include"DB.h"
#include"ServiceObj.h"
#include"WaitObj.h"
#include"ServiceQueue.h"
#include"WaitQueue.h"

#include<ctime>
#include<windows.h>
#include"json.h"

#define MAXSERVER 3

enum class QueueType {
	SERVICE,
	WAIT
};

struct roomInfo {
	int roomId;
	WindMood nowW;//当前风速
	double nowT;//当前房间温度
	double requestT;//目标温度
	int power;// 开关机状态：0(down), 1(on)
};

class Scheduler
{
private:
	//服务队列和等待队列
	ServiceQueue* serviceQueue;
	WaitQueue* waitQueue;
	
	int recordId;//记录record的id号

public:
	Scheduler();
	Scheduler(int i);
	void copeThread();
	static void cope();
	void copeMessage();
	void copeTimeOut(WindMood wind, WaitObj* wait);
	void copeStopWindMessage(Json::Value stopW);
	void copeWindAgainMessage(Json::Value again);
	void copeCommunicationMessage(Json::Value mainP);
	RequestObj searchObj(int roomId);
	struct roomInfo getRoomInfo(int roomId);
	Record createMainpRecord(time_t time, OptionType option, struct roomInfo room, int roomId, int targetT, WindMood targetW);
	Record createSerRecord(RecordType recordType, ServiceObj ser, time_t time, struct roomInfo room, int targetT, WindMood targetW, int roomId);
	bool insertQueue(QueueType queueType, RequestObj* reObj, struct roomInfo room, int targetT, WindMood targetW, int roomId);
	bool deleteQueue(QueueType queueType, RequestObj* reObj, struct roomInfo room, int targetT, WindMood targetW);
	WaitObj* createAndInsertWait(RequestObj* reObj, WindMood targetW, int targetT, struct roomInfo room, int roomId);
	ServiceObj* createAndInsertService(RequestObj* reObj, WindMood targetW, int targetT, struct roomInfo room, int roomId);
	void selectWaitToService();
	void selectServiceToWait();
	time_t getTime();
};

